using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Logic;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 主城靠近离开触发器
    /// </summary>
    public class TrigMCMoveInRange : BaseNonLevelServiceTrigger
    {
        /// <summary>
        /// 触发范围（米）
        /// </summary>
        private float _range;
        /// <summary>
        /// 是否是进入范围触发(0：是，1：不是，2：都可以触发)
        /// </summary>
        private ServiceTriggerMoveRangeType _rangeTriggerType;

        protected override void OnInit()
        {
            base.OnInit();
            if (_configParams.Count != 2)
            {
                LogGame.LogError("移动触发器参数错误，参数1是范围，参数2是类型（0靠近1远离2靠近远离都触发）。实际参数：" + string.Join(',', _configParams));
                return;
            }
            _range = GameMathUtil.LogicValue2Float(_configParams[0]);
            _rangeTriggerType = (ServiceTriggerMoveRangeType)_configParams[1];
            if (_range > MainCityConst.HeroQuadQueryHalfSize)
            {
                LogGame.LogError($"初始化触发器(Type {MainCityTriggerType.MoveInRange})错误：参数0（距离）不能大于{MainCityConst.HeroQuadQueryHalfSize}，否则将不能正确触发。实体mcid：{(_host as MainCityNormalEntity)?.dynNormalEntity.entityConfig.id}。");
            }
        }

        public override bool OnTrigger(object dynamicObjParam, TrigValueTypeParam dynamicValueTypePram)
        {
            if (_range > MainCityConst.HeroQuadQueryHalfSize)
            {
                return false;
            }
            var entity = _host as MainCityNormalEntity;
            if (entity == null)
            {
                LogicLog.LogError("范围移动触发器entity为空，该触发器只对实体生效。");
                return false;
            }
            bool needTriggerIn = false;
            bool needTriggerOut = false;
            var playerAvatar = MainCityModule.Instance.mainController.regionCtrl.entityCtrl.playerAvatarEntity;
            float curSqrDis = (entity.unityPosOfData - playerAvatar.unityPosOfData).sqrMagnitude;
            float prevSqrDis = playerAvatar.GetPrevEntityDisFromMe(entity.id);
            float sqrRange = _range * _range;
            if ((prevSqrDis == -1 || prevSqrDis > sqrRange) && curSqrDis <= sqrRange) //上一次移动不在范围内，现在在了
            {
                needTriggerIn = true;
                LogicLog.Log($"进入实体范围 实体mcid：{entity.dynNormalEntity.entityConfig.id}, range {_range}cm.");
            }
            else if ((prevSqrDis != -1 && prevSqrDis <= sqrRange) && curSqrDis > sqrRange) //上一次移动在范围内，现在出范围了
            {
                needTriggerOut = true;
                LogicLog.Log($"离开实体范围 实体mcid：{entity.dynNormalEntity.entityConfig.id}, range {_range}cm.");
            }

            if (((needTriggerIn || needTriggerOut) && _rangeTriggerType == ServiceTriggerMoveRangeType.AnyWay)
                || (needTriggerIn && _rangeTriggerType == ServiceTriggerMoveRangeType.Approach)
                || (needTriggerOut && _rangeTriggerType == ServiceTriggerMoveRangeType.FarAway))
            {
                return true;
            }
            return false;
        }

        protected override void OnPoolReset()
        {
            _range = 0;
            _rangeTriggerType = 0;
        }
    }
}
